package com.cysd.ewmxns.ui.activity

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.cszy.ewmxns.base.BaseVMActivity
import com.cszy.ewmxns.ext.toast

import com.cysd.ewmxns.ui.dialog.PermissoTipDialog
import com.cysd.ewmxns.ui.dialog.UserRefusedPermissonDialog
import com.cysd.ewmxns.utils.ImageUtil

import com.cysd.ewmxns.utils.SpUtils
import com.cysd.ewmxns.utils.generateQRCode
import com.cysd.ewmxns.viewmodel.MainViewModel
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.hnyyac.ad.mds.utils.BaseAdConstants
import qrrspeeelild.rqrc0ode.andro0ild.R
import qrrspeeelild.rqrc0ode.andro0ild.databinding.ActivityCreatQrCodeResultBinding


class CreatQrCodeResultActivity : BaseVMActivity<MainViewModel, ActivityCreatQrCodeResultBinding>(),IAdViewListener {

    private var pernissonTopdialog: PermissoTipDialog? = null
    private var newGromoreFeedAd : NewGroMoreFeedAdManager? =null
    private var mContent:String?=null;
    override fun initView(savedInstanceState: Bundle?) {
        //mBinding.headTitle.headBack.setImageResource(R.mipmap.ic_back)
        //mBinding.headTitle.headRelative.setBackgroundColor(resources.getColor(R.color.black))
    }


    override fun initData() {
        mContent=intent.getStringExtra("content")
        Log.e("qin","mContent=$mContent")
        mBinding.ivResult.setImageBitmap(generateQRCode(mContent.toString()))
        AdManager.loadChapin(
            this,
            61,
            this,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {
                    newGromoreFeedAd = NewGroMoreFeedAdManager(this@CreatQrCodeResultActivity, BaseAdConstants.FEEDAD_Id, mBinding?.fragmentContainer,62)
                    newGromoreFeedAd?.loadAd()
                }
            })
    }

    override fun initListener() {
        mBinding.headTitle.headBack.setOnClickListener {
            finish()
        }

        mBinding.btnSave.setOnClickListener {
            if (XXPermissions.isGranted(this, android.Manifest.permission.READ_MEDIA_IMAGES)) {
                // 获取相册
                ImageUtil.saveQRCodeToGalleryHighQuality(this, generateQRCode(mContent.toString()))
            } else {
                request_Permission(
                    this,
                    android.Manifest.permission.READ_MEDIA_IMAGES,
                    "相册",
                    "需要获取您的相册权限来选择图片"
                )
            }
        }
    }

    fun request_Permission(
        context: Activity,
        req_permisson: String,
        permisson_name: String,
        content: String
    ) {
        // 检查是否已标记为"不再询问"
        if (!SpUtils.getInstance().getBoolean(permisson_name)) { // 没有获取该权限或者没有永久拒绝
            showPermissonTipsDialog(context, content)
        }
        XXPermissions.with(context)
            // 申请单个权限
            .permission(req_permisson)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (pernissonTopdialog != null) {
                        pernissonTopdialog!!.dismiss()
                    }
                    if (!allGranted) {

                        toast("获取 $permisson_name 权限成功")
                        return
                    }
                    SpUtils.getInstance().putBoolean(permisson_name, true)
                    toast("获取 $permisson_name 权限成功")
                    // 改为调用startScanner()方法
                    ImageUtil.saveQRCodeToGalleryHighQuality(this@CreatQrCodeResultActivity, generateQRCode(mContent.toString()))

                }

                override fun onDenied(
                    permissions: MutableList<String>,
                    doNotAskAgain: Boolean
                ) {
                    if (doNotAskAgain) {
                        if (pernissonTopdialog != null) {
                            pernissonTopdialog!!.dismiss()
                        }
                        showDoNotAskAgainDialog(
                            this@CreatQrCodeResultActivity,
                            req_permisson,
                            permisson_name
                        )
                    } else {
                        if (pernissonTopdialog != null) {
                            pernissonTopdialog!!.dismiss()
                        }
                        finish()
                    }
                }
            })
    }
    /**
     * 用户单次拒绝
     */
    fun showPermissonTipsDialog(context: Context, content: String) {
        pernissonTopdialog = PermissoTipDialog(context)
        pernissonTopdialog!!.setPermissionMessage(content)
        pernissonTopdialog!!.show()
        pernissonTopdialog!!.setCancelable(false)
    }
    /**
     * 用户多次拒绝
     */
    fun showDoNotAskAgainDialog(context: Context, permission: String, permissionName: String) {
        var dialog = UserRefusedPermissonDialog(context,permission)
        dialog.setPermissionMessage("您未授予 $permissionName 权限，请去设置中开启")
        dialog.setCancelable(false)
        dialog.show()
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {
        newGromoreFeedAd = NewGroMoreFeedAdManager(this@CreatQrCodeResultActivity, BaseAdConstants.FEEDAD_Id, mBinding?.fragmentContainer,62)
        newGromoreFeedAd?.loadAd()
    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
}